/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
layui.define(['form', 'pkc', 'format', 'validator'], function (e) {
  var t = layui.$;
  var layform = layui.form;
  var pkc = layui.pkc;
  var format = layui.format;
  var setter = layui.setter;
  var validator = layui.validator;

  var form = {
    local: {
      fm: null //当前页面form元素
    },
    /**
     * 初始化控件事件
     * */
    initEv: function () {
      //监听提交
      layform.on("submit(fmsub)", function (fm) {
        layform.verify();
        layer.confirm("需要保存数据?", {
          icon: 3,
          title: "提示"
        }, function (index) {
          var ldIndex = layer.load(1);
          var data = form.getWebParam(fm);
          form.toolAction.add(data);
          layer.close(index);
        });

        // layer.msg(JSON.stringify(data.field));
        return false;
      });

      t('#btnBack').click(function (e) {
        form.close();
        e.preventDefault();
      });

    },
    /**
     * 界面初始化
     * */
    init: function () {
      // layer.load(2);
      this.initEv();
      layer.closeAll("loading");
    },

    toolAction: {
      // 新增
      add: function (data) {
         return pkc.http.SendPost(data, 'psamcloud.boss.device.psam.add', function (ret) {
          var ret = JSON.parse(ret);
            if (ret) {
              if (ret.Status === "1") {
                //success
                layer.msg('操作成功');
                form.close();
                //重新加载数据
                parent.reloadGrid(10, 1);
                layer.closeAll("loading");
              } else {
                layer.alert(ret.Message);
                layer.closeAll("loading");
              }
            }
          }, function (ret) {
            layer.alert("网络异常.请稍后再试！");
          })
      }

    },
    getWebParam: function (fmData) {
      var data = {};
      data["matrixno"] = fmData.field.matrixno;
      data["matrixip"] = fmData.field.matrixip;
      data["matrixport"] = fmData.field.matrixport;
      data["maxconnectnum"] = parseInt(fmData.field.maxconnectnum);
      data["status"] = parseInt(fmData.field.status);
      return data;
    },
    /**
     * 关闭窗体
     */
    close: function () {
      var index = parent.layer.getFrameIndex(window.name);
      setTimeout(function () {
        parent.layer.close(index);
      }, 1000)
    }
  };

  e("psamadd", {
    init: () => {
      form.init();
    }
  });
});